package greentea.webloginmodule.jaas;

import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author LFilipkiewicz
 * 
 */
public class LoginUtil {

	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	/**
	 * The LoginContext of the JAAS login module.
	 */
	private LoginContext lc = null;

	// must be set before use in other project
	// if not changed and the default config file context not changed the
	// default context name is used - "default".
	public static String loginContext = "default";

	// TODO solve problem with file name and access
	private static final String JAAS_CONF_FILE = "F:\\java\\tomcat\\Tomcat.6.0"
			+ "./webapps/greenreader/WEB-INF/conf/" + LoginUtil.loginContext
			+ "_jaas.config";

	// private static final String JAAS_CONF_FILE =
	// "F:\\java\\tomcat\\Tomcat.6.0/webapps/mdb.web/WEB-INF/conf/mdb_jaas.config";

	public boolean loginUser(String userLogin, String userPassword) {
		boolean retVal = false;

		// TODO move this property setting somewhere else
		System.setProperty("java.security.auth.login.config",
				LoginUtil.JAAS_CONF_FILE);
		logger.debug("Property [java.security.auth.login.config] set to: ["
				+ System.getProperty("java.security.auth.login.config") + "]");

		try {
			// Obtain a LoginContext, needed for authentication. Tell it
			// to use the LoginModule implementation specified by the given
			// entry named in the JAAS login configuration file and to also use
			// the specified CallbackHandler.

			MyCallbackHandler ch = new MyCallbackHandler(userLogin,
					userPassword);
			this.lc = new LoginContext(LoginUtil.loginContext, ch);
			lc.login();
			// if we return with no exception, authentication succeeded
			retVal = true;
		} catch (LoginException le) {
			logger.error("[LoginException].Cannot create LoginContext. "
					+ le.getMessage());
			return false;
		} catch (SecurityException se) {
			logger.error("[SecurityException].Cannot create LoginContext. "
					+ se.getMessage());
			return false;
		}
		return retVal;
	}

	/**
	 * The logout procedure of the WebLoginModule.
	 */
	public void logout() {
		if (lc != null) {
			try {
				lc.logout();
			} catch (LoginException le) {
				logger.error("[LoginException].Cannot create LoginContext. "
						+ le.getMessage());
			} catch (SecurityException se) {
				logger.error("[SecurityException].Cannot create LoginContext. "
						+ se.getMessage());
			}
		} else {
			logger.error("Logout of [null] LoginContext");
		}
	}

	/**
	 * @return the loginContext
	 */
	public static String getLoginContext() {
		return loginContext;
	}

	/**
	 * @param loginContext
	 *            the loginContext to set
	 */
	public static void setLoginContext(String loginContext) {
		LoginUtil.loginContext = loginContext;
	}

}
